package modelo.fornecedor;

import java.util.ArrayList;

import modelo.fornecedor.Fornecedor;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class FornecedorDAO implements FornecedorDAOInterface{

	private static SessionFactory factory; 

	public FornecedorDAO() {
		try{
			factory = new AnnotationConfiguration().
					configure().
					//addPackage("com.xyz") //add package if used.
					addAnnotatedClass(Fornecedor.class).
					buildSessionFactory();
		}catch (Throwable ex) { 
			System.err.println("Falha ao criar o objeto SessionFactory." + ex);
			throw new ExceptionInInitializerError(ex); 
		}  

	}

	@Override
	public Fornecedor getById(int id) {
		Session session = factory.openSession();
		Transaction tx = null;
		Fornecedor fornecedor = null;
		try{
			tx = session.beginTransaction();
			fornecedor = (Fornecedor) session.get(Fornecedor.class, id);
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}

		return fornecedor;
	}

	@Override
	public ArrayList<Fornecedor> getListAll() {
		Session session = factory.openSession();
		Transaction tx = null;
		ArrayList<Fornecedor> fornecedors = null;
		try{
			tx = session.beginTransaction();
			fornecedors = (ArrayList<Fornecedor>) session.createQuery("FROM Fornecedor").list(); 
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
		return fornecedors;

	}

	@Override
	public ArrayList<Fornecedor> getList(String condicao) {
		Session session = factory.openSession();
		Transaction tx = null;
		ArrayList<Fornecedor> fornecedors = null;
		try{
			tx = session.beginTransaction();
			fornecedors = (ArrayList<Fornecedor>) session.createQuery("FROM Fornecedor " + condicao).list(); 
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
		return fornecedors;
	}

	@Override
	public void insert(Fornecedor fornecedor) {
		Session session = factory.openSession();
		Transaction tx = null;

		try{

			tx = session.beginTransaction();
			session.save(fornecedor);
			tx.commit();

		}catch (HibernateException e) {

			if (tx!=null) tx.rollback();
			e.printStackTrace(); 

		}finally {

			session.close(); 

		}

	}

	@Override
	public void delete(int id) {
		Session session = factory.openSession();
		Transaction tx = null;

		try{

			tx = session.beginTransaction();
			session.delete(getById(id));
			tx.commit();

		}catch (HibernateException e) {

			if (tx!=null) tx.rollback();
			e.printStackTrace(); 

		}finally {

			session.close(); 

		}
	}

	@Override
	public void update(Fornecedor fornecedor) {
		Session session = factory.openSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction(); 
			session.update(fornecedor); 

			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
	}

}
